package cn.jtfadmin.base.data.jpa.autoconfig;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.FileSystemUtils;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class ExtJpaRepsitoryClassPathResourceLoaderInitializer implements ApplicationContextInitializer {


    public static final File extClassDir = new File(SystemUtils.getUserDir(), "baseJpaExtClasses");

    @Override
    public void initialize(ConfigurableApplicationContext context) {
        FileSystemUtils.deleteRecursively(extClassDir);
        extClassDir.mkdirs();
        URLClassLoader urlClassLoader = null;
        try {
            urlClassLoader = new URLClassLoader(new URL[]{extClassDir.toURI().toURL()}, context.getClassLoader());
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        context.setClassLoader(urlClassLoader);
    }
}
